import 'package:city_pickers/city_pickers.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:studymanage/core/global.dart';
import 'package:studymanage/core/selectDialog/select_dialog.dart';
import 'package:studymanage/util/request.dart';

import '../home.dart';


class Registered extends StatefulWidget {
  Registered({Key key, this.title}) : super(key: key);

  final String title;

  @override
  State<StatefulWidget> createState() {
    return RegisteredState();
  }
}

class RegisteredState extends State<Registered> {
  final GlobalKey<FormState> _formKey = new GlobalKey<FormState>();
  Map user = new Map();
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        backgroundColor: Colors.white,
        title: Text("填写个人信息",style: TextStyle(color: Color.fromRGBO(51, 51, 51, 1)),),
        leading: FlatButton(
          child: Icon(
            Icons.arrow_back_ios,
            color: Colors.grey,
          ),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
      ),
      body: SingleChildScrollView(
        child:  Container(
            width: MediaQuery.of(context).size.width,
            padding: EdgeInsets.only(
              left: ScreenUtil().setWidth(16.5),
              right: ScreenUtil().setWidth(16.5),
            ),
            color: Colors.white,
            child: Form(
              key: _formKey,
              child: Column(
                children: <Widget>[
                  Container(
                    height: ScreenUtil().setHeight(50),
                    decoration: BoxDecoration(
                      border: Border(
                          bottom: BorderSide(
                            color: Color.fromRGBO(238, 238, 238, 1),
                            width: ScreenUtil().setHeight(0.5),
                          )
                      ),
                    ),
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: <Widget>[
                        Text(
                          '昵称',
                          style: TextStyle(
                            fontFamily: 'PingFangSC-Light',
                            fontSize: ScreenUtil().setSp(14),
                            fontWeight: FontWeight.w600,
                            color: Color.fromRGBO(51, 51, 51, 1),
                          ),
                        ),
                        Container(
                          width: MediaQuery.of(context).size.width - ScreenUtil().setWidth(90),
                          margin: EdgeInsets.only(
                            left: ScreenUtil().setWidth(23.5),
                          ),
                          child: TextFormField(
                            style: TextStyle(
                              fontFamily: 'PingFangSC-Light',
                              fontSize: ScreenUtil().setSp(14),
                              fontWeight: FontWeight.w600,
                              color: Color.fromRGBO(153, 153, 153, 1),
                              height: 1,
                            ),
                            decoration: InputDecoration(
                              border: InputBorder.none,
                            ),
                            onSaved: (v) {
                              setState(() {
                              });
                            },
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    height: ScreenUtil().setHeight(50),
                    decoration: BoxDecoration(
                      border: Border(
                          bottom: BorderSide(
                            color: Color.fromRGBO(238, 238, 238, 1),
                            width: ScreenUtil().setHeight(0.5),
                          )
                      ),
                    ),
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: <Widget>[
                        Text(
                          '姓名',
                          style: TextStyle(
                            fontFamily: 'PingFangSC-Light',
                            fontSize: ScreenUtil().setSp(14),
                            fontWeight: FontWeight.w600,
                            color: Color.fromRGBO(51, 51, 51, 1),
                          ),
                        ),
                        Container(
                          width: MediaQuery.of(context).size.width - ScreenUtil().setWidth(90),
                          margin: EdgeInsets.only(
                            left: ScreenUtil().setWidth(23.5),
                          ),
                          child: TextFormField(
                            style: TextStyle(
                              fontFamily: 'PingFangSC-Light',
                              fontSize: ScreenUtil().setSp(14),
                              fontWeight: FontWeight.w600,
                              color: Color.fromRGBO(153, 153, 153, 1),
                              height: 1,
                            ),
                            decoration: InputDecoration(
                              border: InputBorder.none,
                            ),
                            onSaved: (v) {
                              setState(() {
                                user['name'] = v;
                              });
                            },
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    height: ScreenUtil().setHeight(50),
                    decoration: BoxDecoration(
                      border: Border(
                          bottom: BorderSide(
                            color: Color.fromRGBO(238, 238, 238, 1),
                            width: ScreenUtil().setHeight(0.5),
                          )
                      ),
                    ),
                    child:
                    GestureDetector(
                      behavior: HitTestBehavior.translucent,
                      onTap: (){
                        SelectDialog.showModal<String>(
                          context,
                          showSearchBox: false,
                          items: ['男','女'],
                          onChange: (String selected) {
                            setState(() {
                              if(selected == '男'){
                                user['gender'] ="2";
                              }else{
                                user['gender'] ="3";
                              }
                            });
                          },
                        );
                      },
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: <Widget>[
                          Text(
                            '性别',
                            style: TextStyle(
                              fontFamily: 'PingFangSC-Light',
                              fontSize: ScreenUtil().setSp(14),
                              fontWeight: FontWeight.w600,
                              color: Color.fromRGBO(51, 51, 51, 1),
                            ),
                          ),
                          Container(
                            width: MediaQuery.of(context).size.width - ScreenUtil().setWidth(90),
                            margin: EdgeInsets.only(
                              left: ScreenUtil().setWidth(23.5),
                            ),
                            child: Text(user['gender']!=null ? user['gender'] : "" ),
                          ),
                        ],
                      ),
                    ),
                  ),
                  Container(
                    height: ScreenUtil().setHeight(50),
                    decoration: BoxDecoration(
                      border: Border(
                          bottom: BorderSide(
                            color: Color.fromRGBO(238, 238, 238, 1),
                            width: ScreenUtil().setHeight(0.5),
                          )
                      ),
                    ),
                    child:GestureDetector(
                      onTap: (){
                        CityPickers.showCityPicker(
                          context: context,
                        ).then((value) {
                          print(value);
                          setState(() {
                            user['address'] = value.cityName;
                          });
                        });
                      },
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: <Widget>[
                          Text(
                            '地区',
                            style: TextStyle(
                              fontFamily: 'PingFangSC-Light',
                              fontSize: ScreenUtil().setSp(14),
                              fontWeight: FontWeight.w600,
                              color: Color(0xff333333),
                            ),
                          ),
                          Expanded(
                            child: Container(
                              width: MediaQuery.of(context).size.width - ScreenUtil().setWidth(90),
                              margin: EdgeInsets.only(
                                left: ScreenUtil().setWidth(23.5),
                              ),
                              child: Container(
                                child: Text(user['address']!=null ? user['address'] : "" ),
                              ),
                            ),
                          ),
                          Icon(Icons.expand_more)

                        ],
                      ),
                    ),
                  ),
                  Offstage(
                    offstage: Global.profile.userTypeEnum == 1,
                    child: Container(
                      decoration: BoxDecoration(
                        border: Border(
                            bottom: BorderSide(
                              color: Color.fromRGBO(238, 238, 238, 1),
                              width: ScreenUtil().setHeight(0.5),
                            )
                        ),
                      ),
                      height: ScreenUtil().setHeight(50),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: <Widget>[
                          Text(
                            '级别',
                            style: TextStyle(
                              fontFamily: 'PingFangSC-Light',
                              fontSize: ScreenUtil().setSp(14),
                              fontWeight: FontWeight.w600,
                              color: Color(0xff333333),
                            ),
                          ),
                          Container(
                            width: MediaQuery.of(context).size.width - ScreenUtil().setWidth(90),
                            margin: EdgeInsets.only(
                              left: ScreenUtil().setWidth(23.5),
                            ),
                            child: TextFormField(
                              style: TextStyle(
                                fontFamily: 'PingFangSC-Light',
                                fontSize: ScreenUtil().setSp(14),
                                fontWeight: FontWeight.w600,
                                color: Color.fromRGBO(153, 153, 153, 1),
                                height: 1,
                              ),
                              decoration: InputDecoration(
                                border: InputBorder.none,
                              ),
                              onSaved: (v) {
                                user['address'] =v;
                              },
                            ),
                          ),

                        ],
                      ),
                    ),
                  ),
                  Offstage(
                    offstage: Global.profile.userTypeEnum == 1,
                    child: Container(
                      decoration: BoxDecoration(
                        border: Border(
                            bottom: BorderSide(
                              color: Color.fromRGBO(238, 238, 238, 1),
                              width: ScreenUtil().setHeight(0.5),
                            )
                        ),
                      ),
                      height: ScreenUtil().setHeight(50),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: <Widget>[
                          Text(
                            '简介',
                            style: TextStyle(
                              fontFamily: 'PingFangSC-Light',
                              fontSize: ScreenUtil().setSp(14),
                              fontWeight: FontWeight.w600,
                              color: Color(0xff333333),
                            ),
                          ),
                          Container(
                            width: MediaQuery.of(context).size.width - ScreenUtil().setWidth(90),
                            margin: EdgeInsets.only(
                              left: ScreenUtil().setWidth(23.5),
                            ),
                            child: TextFormField(
                              style: TextStyle(
                                fontFamily: 'PingFangSC-Light',
                                fontSize: ScreenUtil().setSp(14),
                                fontWeight: FontWeight.w600,
                                color: Color.fromRGBO(153, 153, 153, 1),
                                height: 1,
                              ),
                              decoration: InputDecoration(
                                border: InputBorder.none,
                              ),
                              onSaved: (v) {
                                user['address'] =v;
                              },
                            ),
                          ),

                        ],
                      ),
                    ),
                  ),
                  Offstage(
                    offstage: Global.profile.userTypeEnum == 2,
                    child: Container(
                      decoration: BoxDecoration(
                        border: Border(
                            bottom: BorderSide(
                              color: Color.fromRGBO(238, 238, 238, 1),
                              width: ScreenUtil().setHeight(0.5),
                            )
                        ),
                      ),
                      height: ScreenUtil().setHeight(50),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: <Widget>[
                          Text(
                              '班级',
                            style: TextStyle(
                              fontFamily: 'PingFangSC-Light',
                              fontSize: ScreenUtil().setSp(14),
                              fontWeight: FontWeight.w600,
                              color: Color(0xff333333),
                            ),
                          ),
                          Container(
                            width: MediaQuery.of(context).size.width - ScreenUtil().setWidth(90),
                            margin: EdgeInsets.only(
                              left: ScreenUtil().setWidth(23.5),
                            ),
                            child: TextFormField(
                              style: TextStyle(
                                fontFamily: 'PingFangSC-Light',
                                fontSize: ScreenUtil().setSp(14),
                                fontWeight: FontWeight.w600,
                                color: Color.fromRGBO(153, 153, 153, 1),
                                height: 1,
                              ),
                              decoration: InputDecoration(
                                border: InputBorder.none,
                              ),
                              onSaved: (v) {
                                user['address'] =v;
                              },
                            ),
                          ),

                        ],
                      ),
                    ),
                  ),
                  Offstage(
                    offstage: Global.profile.userTypeEnum == 2,
                    child: Container(
                      decoration: BoxDecoration(
                        border: Border(
                            bottom: BorderSide(
                              color: Color.fromRGBO(238, 238, 238, 1),
                              width: ScreenUtil().setHeight(0.5),
                            )
                        ),
                      ),
                      height: ScreenUtil().setHeight(50),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: <Widget>[
                          Text(
                            '导师',
                            style: TextStyle(
                              fontFamily: 'PingFangSC-Light',
                              fontSize: ScreenUtil().setSp(14),
                              fontWeight: FontWeight.w600,
                              color: Color(0xff333333),
                            ),
                          ),
                          Container(
                            width: MediaQuery.of(context).size.width - ScreenUtil().setWidth(90),
                            margin: EdgeInsets.only(
                              left: ScreenUtil().setWidth(23.5),
                            ),
                            child: TextFormField(
                              style: TextStyle(
                                fontFamily: 'PingFangSC-Light',
                                fontSize: ScreenUtil().setSp(14),
                                fontWeight: FontWeight.w600,
                                color: Color.fromRGBO(153, 153, 153, 1),
                                height: 1,
                              ),
                              decoration: InputDecoration(
                                border: InputBorder.none,
                              ),
                              onSaved: (v) {
                                user['address'] =v;
                              },
                            ),
                          ),

                        ],
                      ),
                    ),
                  ),
                  Offstage(
                    offstage: Global.profile.userTypeEnum == 2,
                    child: Container(
                      decoration: BoxDecoration(
                        border: Border(
                            bottom: BorderSide(
                              color: Color.fromRGBO(238, 238, 238, 1),
                              width: ScreenUtil().setHeight(0.5),
                            )
                        ),
                      ),
                      height: ScreenUtil().setHeight(50),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: <Widget>[
                          Text(
                            '学员简介',
                            style: TextStyle(
                              fontFamily: 'PingFangSC-Light',
                              fontSize: ScreenUtil().setSp(14),
                              fontWeight: FontWeight.w600,
                              color: Color(0xff333333),
                            ),
                          ),
                          Expanded(
                            child: Container(
                              width: MediaQuery.of(context).size.width - ScreenUtil().setWidth(90),
                              margin: EdgeInsets.only(
                                left: ScreenUtil().setWidth(23.5),
                              ),
                              child: TextFormField(
                                style: TextStyle(
                                  fontFamily: 'PingFangSC-Light',
                                  fontSize: ScreenUtil().setSp(14),
                                  fontWeight: FontWeight.w600,
                                  color: Color.fromRGBO(153, 153, 153, 1),
                                  height: 1,
                                ),
                                decoration: InputDecoration(
                                  border: InputBorder.none,
                                ),
                                onSaved: (v) {
                                  user['intro'] =v;
                                },
                              ),
                            ),
                          ),

                        ],
                      ),
                    ),
                  ),
                  Container(
                    height: ScreenUtil().setHeight(44),
                    margin: EdgeInsets.only(
                      top: ScreenUtil().setHeight(13),
                      bottom: ScreenUtil().setHeight(13),
                    ),
                    decoration: BoxDecoration(
                      color: Color.fromRGBO(214, 178, 128, 1),
                      borderRadius: BorderRadius.all(
                        Radius.circular(ScreenUtil().setHeight(4)),
                      ),
                    ),
                    child: GestureDetector(
                      child: Container(
                        alignment: Alignment.center,
                        width: MediaQuery.of(context).size.width,
                        margin: EdgeInsets.only(
                          left: ScreenUtil().setHeight(8.5),
                        ),
                        child: Text(
                          '提交',
                          style: TextStyle(
                            fontFamily: 'PingFangSC-Regular',
                            fontSize: ScreenUtil().setSp(16),
                            color: Colors.white,
                          ),
                        ),
                      ),
                      onTap: (){
                        _formKey.currentState.save();
                        Request().post("/multiapi/c213v_userData",data: {
                          "nickname":user['nickname'],
                          "avatar": user['avatar'],
                          "name": user['name'],
                          "address": user['address'],
                          "intro": user['intro'],
                        }).then((value){
                          Navigator.pushAndRemoveUntil(context,
                              MaterialPageRoute(
                                  builder: (context) =>
                                      Home(Global.profile.userTypeEnum == 1
                                          ? Role.student
                                          : Role.teacher)), (
                                  route) => false);
                        });
                      },
                    ),
                  ),
                ],
              ),
            )
        ),
      )
    );
  }

  @override
  void dispose() {
    super.dispose();
  }
}
